import {createStore, applyMiddleware} from 'redux';
import {persistStore, persistReducer} from 'redux-persist';
import reducers from './reducers';
import {sagas} from './sagas';
import AsyncStorage from '@react-native-async-storage/async-storage';

const createSagaMiddleWare = require('redux-saga').default;
const middlewares = [];
const sagaMiddleWare = createSagaMiddleWare();
middlewares.push(sagaMiddleWare);

const {logger} = require('redux-logger');

if (__DEV__) {
    middlewares.push(logger);
}

const createStoreWithMiddleware = applyMiddleware(...middlewares)(createStore);
const persistedReducer = persistReducer({
    key: 'rootReducers',
    storage: AsyncStorage,
}, reducers);

const store = createStoreWithMiddleware(persistedReducer);
export const persistor = persistStore(store, {}, () => {
    sagaMiddleWare.run(sagas);
});
export default store;
